﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.EnhancedEntities;
using MovieRental_Presentation.Helpers;
using MovieRental.Entities;
using System.Collections.ObjectModel;

namespace MovieRental_Presentation.DataViews
{
    public class MovieDetailsDataView : BaseDataView
    {
        private ObservableCollection<Edition> _editionsList = new ObservableCollection<Edition>();
        private Movie _movie;
        private bool _isReadOnly;
        private Edition _selectedEdition;
        private double _discountValue;
        private DateTime _validUntil;


        public MovieDetailsDataView(Guid movieId)
        {
            _movie = new Movie(ComponentManager.Instance.MovieComponent.Get(movieId));
            ComponentManager.Instance.EditionComponent.SourceUpdated += MakeEventHandlerForProperty("EditionsList");
            ComponentManager.Instance.EditionComponent.SourceUpdated += MakeEventHandlerForProperty("Movie");
            ComponentManager.Instance.TemporaryDiscountComponent.SourceUpdated += MakeEventHandlerForProperty("EditionsList");
            ComponentManager.Instance.LoanCartComponent.SourceUpdated += MakeEventHandlerForProperty("TopMovies");
            ComponentManager.Instance.LoanCartComponent.SourceUpdated += MakeEventHandlerForProperty("BottomMovies");
        }

        public Movie Movie
        {
            set { SetProperty<Movie>(ref _movie, value, "Movie"); }
            get { return _movie; }  
        }

        public bool IsReadOnly
        {
            set { SetProperty<bool>(ref _isReadOnly, value, "IsReadOnly"); }
            get { return _isReadOnly; }
        }

        public ObservableCollection<Edition> EditionsList
        {
            get
            {
                var query = ComponentManager.Instance.EditionComponent.GetAllEditionsForMovie(Movie.Entity.Id).Select(x => new Edition(x));
                UpdateObservableCollection<Edition>(_editionsList, query);
                return _editionsList;
            }
        }

        public IEnumerable<string> MediaTypes
        {
            get
            {
                return ComponentManager.Instance.MediaTypeComponent.GetAll().Select(x => x.Name);
            }
        }

        private void UpdateDiscountInfo()
        {
            if (SelectedEdition == null)
                return;
            var discount = ComponentManager.Instance.TemporaryDiscountComponent.GetAvailableDiscount(SelectedEdition.Entity);
            DiscountValue = (discount == null) ? 0 : discount.Value;
            DiscountValidUntil = (discount == null) ? DateTime.Today : discount.ValidUntil;        
        }

        public Edition SelectedEdition
        {
            set 
            { 
                SetProperty<Edition>(ref _selectedEdition, value, "SelectedEdition");
                UpdateDiscountInfo();
            }
            get
            {
                if (_selectedEdition == null)
                    return null;
                if (_selectedEdition.Entity.MovieId == Guid.Empty)
                    _selectedEdition.Entity.MovieId = Movie.Entity.Id;
                return _selectedEdition;
            }
        }

        public double DiscountValue
        {
            set { SetProperty<double>(ref _discountValue, value, "DiscountValue"); }
            get { return _discountValue; }
        }

        public DateTime DiscountValidUntil
        {
            set { SetProperty<DateTime>(ref _validUntil, value, "DiscountValidUntil"); }
            get { return _validUntil; }
        }
    }
}
